home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / nevow / wsgi.py < prev    next >
Text File  |  2008-01-02  |  22KB  |  616 lines

  1. # TODO:
  2. #  1. make exception renderer work (currently the code is in appserver.py)
  3. # - srid
  4.  
  5. import warnings
  6. warnings.warn("nevow.wsgi is deprecated.", category=DeprecationWarning)
  7.  
  8. import sys, socket, math, time
  9. import cgi # for FieldStorage
  10. import types
  11. from urllib import unquote, quote
  12.  
  13. from zope.interface import implements
  14.  
  15. from twisted.web.http import stringToDatetime
  16.  
  17. from nevow import context, flat, inevow, util
  18. from nevow import __version__ as nevowversion
  19.  
  20. def log(msg):
  21.     print >>sys.stderr, "WSGI: {%s}" % str(msg)
  22.  
  23. errorMarker = object()
  24.  
  25. class NevowWSGISite(object):
  26.  
  27.     def __init__(self, request, resource):
  28.         self.request = request
  29.         self.resource = resource
  30.         self.context = context.SiteContext()
  31.  
  32.     def remember(self, obj, inter=None):
  33.         self.context.remember(obj, inter)
  34.  
  35.     def getPageContextForRequestContext(self, ctx):
  36.         """Retrieve a resource from this site for a particular request. The
  37.         resource will be wrapped in a PageContext which keeps track
  38.         of how the resource was located.
  39.         """
  40.         path = inevow.IRemainingSegments(ctx)
  41.         res = inevow.IResource(self.resource)
  42.         pageContext = context.PageContext(tag=res, parent=ctx)
  43.         return self.handleSegment(
  44.             res.locateChild(pageContext, path),
  45.             ctx.tag, path, pageContext)
  46.  
  47.     def handleSegment(self, result, request, path, pageContext):
  48.         if result is errorMarker:
  49.             return errorMarker
  50.  
  51.         newres, newpath = result
  52.         
  53.         # If the child resource is None then display a 404 page
  54.         if newres is None:
  55.             from nevow.rend import FourOhFour
  56.             return context.PageContext(tag=FourOhFour(), parent=pageContext)
  57.  
  58.         # If we got a deferred then we need to call back later, once the
  59.         # child is actually available.
  60.         #if isinstance(newres, defer.Deferred):
  61.         #    return newres.addCallback(
  62.         #        lambda actualRes: self.handleSegment(
  63.         #            (actualRes, newpath), request, path, pageContext))
  64.  
  65.         newres = inevow.IResource(newres, persist=True)
  66.         if newres is pageContext.tag:
  67.             assert not newpath is path, "URL traversal cycle detected when attempting to locateChild %r from resource %r." % (path, pageContext.tag)
  68.             assert  len(newpath) < len(path), "Infinite loop impending..."
  69.  
  70.         ## We found a Resource... update the request.prepath and postpath
  71.         for x in xrange(len(path) - len(newpath)):
  72.             request.prepath.append(request.postpath.pop(0))
  73.  
  74.         ## Create a context object to represent this new resource
  75.         ctx = context.PageContext(tag=newres, parent=pageContext)
  76.         ctx.remember(tuple(request.prepath), inevow.ICurrentSegments)
  77.         ctx.remember(tuple(request.postpath), inevow.IRemainingSegments)
  78.  
  79.         res = newres
  80.         path = newpath
  81.  
  82.         if not path:
  83.             return ctx
  84.  
  85.         return self.handleSegment(
  86.                 res.locateChild(ctx, path),
  87.                 request, path, ctx)
  88.  
  89.  
  90.     
  91. def createWSGIApplication(page, rootURL=None):
  92.     """Given a Page instance, return a WSGI callable.
  93.     `rootURL` - URL to be remembered as root
  94.     """
  95.     page.flattenFactory = flat.iterflatten
  96.     siteCtx = context.SiteContext(tag=None)
  97.     def application(environ, start_response):
  98.         request = WSGIRequest(environ, start_response)
  99.         prefixURL = rootURL
  100.         if prefixURL is None:
  101.             # Try to guess
  102.             proto = request.isSecure() and 'https://' or 'http://'
  103.             server = environ['SERVER_NAME']
  104.             prefixURL = proto + server + environ.get('SCRIPT_NAME', '/')
  105.         request.rememberRootURL(prefixURL)
  106.         site = NevowWSGISite(request, page)
  107.         request.site = site
  108.         result = request.process()
  109.         
  110.         if not request.headersSent:
  111.             request.write('') # send headers now
  112.         if isinstance(result, str):
  113.             yield str(result) # work around wsgiref using StringType 
  114.         elif isinstance(result, util.Deferred):
  115.             ## So we can use the wsgi module if twisted is installed
  116.             ## TODO use render synchronously instead maybe? I'm pretty
  117.             ## sure after the application callable returns, the request
  118.             ## is "closed". Investigate with the latest wsgi spec and
  119.             ## some implementations.
  120.             #raise 'PH' + str(dir(result)) + '{{%s}}' % str(result.result)
  121.             yield result.result
  122.         else:
  123.             for x in result:
  124.                 yield x
  125.             
  126.     return application
  127.  
  128.     
  129. # TODO: convert interface comments
  130. class WSGIRequest(object):
  131.     implements(inevow.IRequest)
  132.     
  133.     """A HTTP request.
  134.  
  135.     Subclasses should override the process() method to determine how
  136.     the request will be processed.
  137.     
  138.     @ivar method: The HTTP method that was used.
  139.     @ivar uri: The full URI that was requested (includes arguments).
  140.     @ivar path: The path only (arguments not included).
  141.     @ivar args: All of the arguments, including URL and POST arguments.
  142.     @type args: A mapping of strings (the argument names) to lists of values.
  143.     @ivar received_headers: All received headers
  144.     """
  145.  
  146.     def __init__(self, environ, start_response):
  147.         self.environ = environ
  148.         self.start_response = start_response
  149.         self.outgoingHeaders = []
  150.         self.received_headers = {}
  151.         self.lastModified = None
  152.         self.etag = None
  153.         self.method = environ.get('REQUEST_METHOD', 'GET')
  154.         self.args = self._parseQuery(environ.get('QUERY_STRING', ''))
  155.         try:
  156.             self.host = (self.environ['REMOTE_ADDR'], int(self.environ['REMOTE_PORT']))
  157.         except KeyError:
  158.             pass # TODO
  159.         for k,v in environ.items():
  160.             if k.startswith('HTTP_'):
  161.                 self.received_headers[k[5:].lower()] = v
  162.         self.setResponseCode("200")
  163.         self.headersSent = False
  164.         self.appRootURL = None
  165.         self.deferred = util.Deferred()
  166.  
  167.     def process(self):
  168.         """When a form is POSTed,
  169.         we create a cgi.FieldStorage instance using the data posted,
  170.         and set it as the request.fields attribute. This way, we can
  171.         get at information about filenames and mime-types of
  172.         files that were posted."""
  173.         if self.method == 'POST':
  174.             self.fields = cgi.FieldStorage(
  175.                             self.environ['wsgi.input'],
  176.                             self.received_headers, 
  177.                             environ={'REQUEST_METHOD': 'POST'})
  178.  
  179.         # set various default headers
  180.         self.setHeader('server', nevowversion)
  181.         year, month, day, hh, mm, ss, wd, y, z = time.gmtime(time.time())
  182.         # HTTP date string format
  183.         s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
  184.                 weekdayname[wd],
  185.                 day, monthname[month], year,
  186.                 hh, mm, ss)
  187.         self.setHeader('date', s)
  188.         self.setHeader('content-type', 'text/html; charset=UTF-8')
  189.  
  190.         # Resource Identification
  191.         self.prepath = []
  192.         self.postpath = map(unquote, self.path[1:].split('/'))
  193.         self.sitepath = []
  194.  
  195.         requestContext = context.RequestContext(
  196.             parent=self.site.context, tag=self)
  197.         requestContext.remember( (), inevow.ICurrentSegments)
  198.         requestContext.remember(tuple(self.postpath), inevow.IRemainingSegments)
  199.  
  200.         pageContext = self.site.getPageContextForRequestContext(requestContext)
  201.  
  202.         return self.gotPageContext(pageContext)
  203.  
  204.     def gotPageContext(self, pageContext):
  205.         if pageContext is errorMarker:
  206.             return None
  207.         html = pageContext.tag.renderHTTP(pageContext)
  208.         if isinstance(html, util.Deferred):
  209.             # This is a deferred object
  210.             # Let us return it synchronously
  211.             # (wsgi has nothing to do with sync, async)
  212.             # XXX: Is this correct?
  213.             html = html.result
  214.         
  215.         # FIXME: I dunno what to do when a generator comes ..
  216.         #      Perhaps, it may generate non-str? I dunno
  217.         if type(html) is types.GeneratorType:
  218.             html = ''.join(list(html))
  219.  
  220.         if html is errorMarker:
  221.             ## Error webpage has already been rendered and finish called
  222.             pass
  223.         elif isinstance(html, str):
  224.             return html
  225.         else:
  226.             res = inevow.IResource(html, None)
  227.             if res is not None:
  228.                 pageContext = context.PageContext(tag=res, parent=pageContext)
  229.                 return self.gotPageContext(pageContext)
  230.             else:              
  231.                 # import traceback; traceback.print_stack()
  232.                 print >>sys.stderr, "html is not a string: %s on %s" % (str(html), pageContext.tag)
  233.         return html
  234.  
  235.     def _parseQuery(self, qs):
  236.         d = {}
  237.         items = [s2 for s1 in qs.split("&") for s2 in s1.split(";")]
  238.         for item in items:
  239.             try:
  240.                 k, v = item.split("=", 1)
  241.             except ValueError:
  242.                 # no strict parsing
  243.                 continue
  244.             k = unquote(k.replace("+", " "))
  245.             v = unquote(v.replace("+", " "))
  246.             if k in d:
  247.                 d[k].append(v)
  248.             else:
  249.                 d[k] = [v]
  250.         return d
  251.  
  252.     def _getPath(self):
  253.         pth = self.environ.get('PATH_INFO', '')
  254.         if not pth: pth = '/'
  255.         return pth
  256.     path = property(_getPath)
  257.     
  258.     def _getURI(self):
  259.         query = self.environ.get('QUERY_STRING', '')
  260.         if query:
  261.             query = '?' + query
  262.         uri = self.path + query
  263.         log('URL:'+uri)
  264.         return uri
  265.     uri = property(_getURI)
  266.     
  267.     # Methods for received request
  268.     def getHeader(self, key):
  269.         """Get a header that was sent from the network.
  270.         """
  271.         return self.received_headers.get(key.lower())
  272.         
  273.     def getCookie(self, key):
  274.         """Get a cookie that was sent from the network.
  275.         """ 
  276.  
  277.  
  278.     def getAllHeaders(self):
  279.         """Return dictionary of all headers the request received."""
  280.         return self.received_headers
  281.  
  282.     def getRequestHostname(self):
  283.         """Get the hostname that the user passed in to the request.
  284.  
  285.         This will either use the Host: header (if it is available) or the
  286.         host we are listening on if the header is unavailable.
  287.         """
  288.         return (self.getHeader('host') or
  289.                 socket.gethostbyaddr(self.getHost()[1])[0]
  290.                 ).split(':')[0]
  291.  
  292.  
  293.     def getHost(self):
  294.         """Get my originally requesting transport's host.
  295.  
  296.         Don't rely on the 'transport' attribute, since Request objects may be
  297.         copied remotely.  For information on this method's return value, see
  298.         twisted.internet.tcp.Port.
  299.         """
  300.         
  301.     def getClientIP(self):
  302.         return self.environ.get('REMOTE_ADDR', None)
  303.         
  304.     def getClient(self):
  305.         pass
  306.     def getUser(self):
  307.         pass
  308.     def getPassword(self):
  309.         pass
  310.     def isSecure(self):
  311.         return self.environ['wsgi.url_scheme'] == 'https'
  312.  
  313.     def getSession(self, sessionInterface = None):
  314.         pass
  315.     
  316.     def URLPath(self):
  317.         from nevow import url
  318.         return url.URL.fromString(self.appRootURL+self.uri)
  319.  
  320.     def prePathURL(self):
  321.         if self.isSecure():
  322.             default = 443
  323.         else:
  324.             default = 80
  325.         # TODO: use getHost().port after getHost is implemented
  326.         port = default 
  327.         if port == default:
  328.             hostport = ''
  329.         else:
  330.             hostport = ':%d' % port
  331.         # FIXME: This hack, until url module is fixed to support RootURLs
  332.         #        Or is this the right way to do?
  333.         if self.appRootURL:
  334.             if self.appRootURL[-1] == '/':
  335.                 rootURL = self.appRootURL
  336.             else:
  337.                 rootURL = self.appRootURL + '/' # yuck!
  338.             return quote('%s%s' % (rootURL, 
  339.                             '/'.join(self.prepath)),
  340.                         '/:')
  341.         return quote('http%s://%s%s/%s' % (
  342.             self.isSecure() and 's' or '',
  343.             self.getRequestHostname(),
  344.             hostport,
  345.             '/'.join(self.prepath)), '/:')
  346.  
  347.     def rememberRootURL(self, url=None):
  348.         # result = p.renderHTTP(pctx)
  349.         """
  350.         Remember the currently-processed part of the URL for later
  351.         recalling.
  352.         """
  353.         if url is None:
  354.             raise NotImplementedError
  355.         self.appRootURL = url
  356.         
  357.     def getRootURL(self):
  358.         """
  359.         Get a previously-remembered URL.
  360.         """
  361.         return self.appRootURL
  362.         
  363.     # Methods for outgoing request
  364.     def finish(self):
  365.         """We are finished writing data."""
  366.  
  367.     def write(self, data):
  368.         """
  369.         Write some data as a result of an HTTP request.  The first
  370.         time this is called, it writes out response data.
  371.         """
  372.         if self.headersSent:
  373.             self._write(data)
  374.             return
  375.         headerkeys = [k for k,v in self.outgoingHeaders]
  376.         self._write = self.start_response(
  377.                     self.responseCode, self.outgoingHeaders, None)
  378.         self.headersSent = True
  379.         if data:
  380.             self._write(data)
  381.             
  382.     def addCookie(self, k, v, expires=None, domain=None, path=None, max_age=None, comment=None, secure=None):
  383.         """Set an outgoing HTTP cookie.
  384.  
  385.         In general, you should consider using sessions instead of cookies, see
  386.         twisted.web.server.Request.getSession and the
  387.         twisted.web.server.Session class for details.
  388.         """
  389.  
  390.     def setResponseCode(self, code, message=None):
  391.         """Set the HTTP response code.
  392.         """
  393.         self.responseCode = '%s %s' % (code, RESPONSES[int(str(code))])
  394.  
  395.     def setHeader(self, header, value):
  396.         """Set an outgoing HTTP header.
  397.         """
  398.         self.outgoingHeaders.append((header.lower(), value))
  399.  
  400.     def redirect(self, url):
  401.         """Utility function that does a redirect.
  402.  
  403.         The request should have finish() called after this.
  404.         """
  405.         log('REDIRECT to ' + str(url))
  406.         self.setResponseCode(str(302))
  407.         self.setHeader('location', url)
  408.  
  409.     def setLastModified(self, when):
  410.         """Set the X{Last-Modified} time for the response to this request.
  411.  
  412.         If I am called more than once, I ignore attempts to set
  413.         Last-Modified earlier, only replacing the Last-Modified time
  414.         if it is to a later value.
  415.  
  416.         If I am a conditional request, I may modify my response code
  417.         to L{NOT_MODIFIED} if appropriate for the time given.
  418.  
  419.         @param when: The last time the resource being returned was
  420.             modified, in seconds since the epoch.
  421.         @type when: number
  422.         @return: If I am a X{If-Modified-Since} conditional request and
  423.             the time given is not newer than the condition, I return
  424.             L{http.CACHED<CACHED>} to indicate that you should write no
  425.             body.  Otherwise, I return a false value.
  426.         """
  427.         # time.time() may be a float, but the HTTP-date strings are
  428.         # only good for whole seconds.
  429.         when = long(math.ceil(when))
  430.         if (not self.lastModified) or (self.lastModified < when):
  431.             self.lastModified = when
  432.  
  433.         modified_since = self.getHeader('if-modified-since')
  434.         if modified_since:
  435.             modified_since = stringToDatetime(modified_since)
  436.             if modified_since >= when:
  437.                 self.setResponseCode(NOT_MODIFIED)
  438.                 return '' # TODO: return http.CACHED (requires Twisted)
  439.         return None
  440.  
  441.     def setETag(self, etag):
  442.         """Set an X{entity tag} for the outgoing response.
  443.  
  444.         That's \"entity tag\" as in the HTTP/1.1 X{ETag} header, \"used
  445.         for comparing two or more entities from the same requested
  446.         resource.\"
  447.  
  448.         If I am a conditional request, I may modify my response code
  449.         to L{NOT_MODIFIED<twisted.protocols.http.NOT_MODIFIED>} or
  450.         L{PRECONDITION_FAILED<twisted.protocols.http.PRECONDITION_FAILED>},
  451.         if appropriate for the tag given.
  452.  
  453.         @param etag: The entity tag for the resource being returned.
  454.         @type etag: string
  455.         @return: If I am a X{If-None-Match} conditional request and
  456.             the tag matches one in the request, I return
  457.             L{CACHED<twisted.protocols.http.CACHED>} to indicate that
  458.             you should write no body.  Otherwise, I return a false
  459.             value.
  460.         """
  461.         if etag:
  462.             self.etag = etag
  463.  
  464.         tags = self.getHeader("if-none-match")
  465.         if tags:
  466.             tags = tags.split()
  467.             if (etag in tags) or ('*' in tags):
  468.                 self.setResponseCode(((self.method in ("HEAD", "GET"))
  469.                                       and NOT_MODIFIED)
  470.                                      or PRECONDITION_FAILED)
  471.                 return '' # TODO: return http.CACHED (requires Twisted)
  472.         return None
  473.  
  474.     def setHost(self, host, port, ssl=0):
  475.         """Change the host and port the request thinks it's using.
  476.  
  477.         This method is useful for working with reverse HTTP proxies (e.g.
  478.         both Squid and Apache's mod_proxy can do this), when the address
  479.         the HTTP client is using is different than the one we're listening on.
  480.  
  481.         For example, Apache may be listening on https://www.example.com, and then
  482.         forwarding requests to http://localhost:8080, but we don't want HTML produced
  483.         by Twisted to say 'http://localhost:8080', they should say 'https://www.example.com',
  484.         so we do:
  485.  
  486.         >>> request.setHost('www.example.com', 443, ssl=1)
  487.  
  488.         This method is experimental.
  489.         """
  490.  
  491.     # Methods not part of IRequest
  492.     #
  493.  
  494.     producer = None
  495.     def registerProducer(self, other, _):
  496.         assert self.producer is None
  497.         self.producer = other
  498.         while self.producer is not None:
  499.             self.producer.resumeProducing()
  500.  
  501.     def unregisterProducer(self):
  502.         self.producer = None
  503.  
  504.     def finish(self):
  505.         self.deferred.callback('')
  506.  
  507. # FIXME: copied from twisted.web.http
  508. _CONTINUE = 100
  509. SWITCHING = 101
  510.  
  511. OK                              = 200
  512. CREATED                         = 201
  513. ACCEPTED                        = 202
  514. NON_AUTHORITATIVE_INFORMATION   = 203
  515. NO_CONTENT                      = 204
  516. RESET_CONTENT                   = 205
  517. PARTIAL_CONTENT                 = 206
  518. MULTI_STATUS                    = 207
  519.  
  520. MULTIPLE_CHOICE                 = 300
  521. MOVED_PERMANENTLY               = 301
  522. FOUND                           = 302
  523. SEE_OTHER                       = 303
  524. NOT_MODIFIED                    = 304
  525. USE_PROXY                       = 305
  526. TEMPORARY_REDIRECT              = 307
  527.  
  528. BAD_REQUEST                     = 400
  529. UNAUTHORIZED                    = 401
  530. PAYMENT_REQUIRED                = 402
  531. FORBIDDEN                       = 403
  532. NOT_FOUND                       = 404
  533. NOT_ALLOWED                     = 405
  534. NOT_ACCEPTABLE                  = 406
  535. PROXY_AUTH_REQUIRED             = 407
  536. REQUEST_TIMEOUT                 = 408
  537. CONFLICT                        = 409
  538. GONE                            = 410
  539. LENGTH_REQUIRED                 = 411
  540. PRECONDITION_FAILED             = 412
  541. REQUEST_ENTITY_TOO_LARGE        = 413
  542. REQUEST_URI_TOO_LONG            = 414
  543. UNSUPPORTED_MEDIA_TYPE          = 415
  544. REQUESTED_RANGE_NOT_SATISFIABLE = 416
  545. EXPECTATION_FAILED              = 417
  546.  
  547. INTERNAL_SERVER_ERROR           = 500
  548. NOT_IMPLEMENTED                 = 501
  549. BAD_GATEWAY                     = 502
  550. SERVICE_UNAVAILABLE             = 503
  551. GATEWAY_TIMEOUT                 = 504
  552. HTTP_VERSION_NOT_SUPPORTED      = 505
  553. INSUFFICIENT_STORAGE_SPACE      = 507
  554. NOT_EXTENDED                    = 510
  555.  
  556. RESPONSES = {
  557.     # 100
  558.     _CONTINUE: "Continue",
  559.     SWITCHING: "Switching Protocols",
  560.  
  561.     # 200
  562.     OK: "OK",
  563.     CREATED: "Created",
  564.     ACCEPTED: "Accepted",
  565.     NON_AUTHORITATIVE_INFORMATION: "Non-Authoritative Information",
  566.     NO_CONTENT: "No Content",
  567.     RESET_CONTENT: "Reset Content.",
  568.     PARTIAL_CONTENT: "Partial Content",
  569.     MULTI_STATUS: "Multi-Status",
  570.  
  571.     # 300
  572.     MULTIPLE_CHOICE: "Multiple Choices",
  573.     MOVED_PERMANENTLY: "Moved Permanently",
  574.     FOUND: "Found",
  575.     SEE_OTHER: "See Other",
  576.     NOT_MODIFIED: "Not Modified",
  577.     USE_PROXY: "Use Proxy",
  578.     # 306 not defined??
  579.     TEMPORARY_REDIRECT: "Temporary Redirect",
  580.  
  581.     # 400
  582.     BAD_REQUEST: "Bad Request",
  583.     UNAUTHORIZED: "Unauthorized",
  584.     PAYMENT_REQUIRED: "Payment Required",
  585.     FORBIDDEN: "Forbidden",
  586.     NOT_FOUND: "Not Found",
  587.     NOT_ALLOWED: "Method Not Allowed",
  588.     NOT_ACCEPTABLE: "Not Acceptable",
  589.     PROXY_AUTH_REQUIRED: "Proxy Authentication Required",
  590.     REQUEST_TIMEOUT: "Request Time-out",
  591.     CONFLICT: "Conflict",
  592.     GONE: "Gone",
  593.     LENGTH_REQUIRED: "Length Required",
  594.     PRECONDITION_FAILED: "Precondition Failed",
  595.     REQUEST_ENTITY_TOO_LARGE: "Request Entity Too Large",
  596.     REQUEST_URI_TOO_LONG: "Request-URI Too Long",
  597.     UNSUPPORTED_MEDIA_TYPE: "Unsupported Media Type",
  598.     REQUESTED_RANGE_NOT_SATISFIABLE: "Requested Range not satisfiable",
  599.     EXPECTATION_FAILED: "Expectation Failed",
  600.  
  601.     # 500
  602.     INTERNAL_SERVER_ERROR: "Internal Server Error",
  603.     NOT_IMPLEMENTED: "Not Implemented",
  604.     BAD_GATEWAY: "Bad Gateway",
  605.     SERVICE_UNAVAILABLE: "Service Unavailable",
  606.     GATEWAY_TIMEOUT: "Gateway Time-out",
  607.     HTTP_VERSION_NOT_SUPPORTED: "HTTP Version not supported",
  608.     INSUFFICIENT_STORAGE_SPACE: "Insufficient Storage Space",
  609.     NOT_EXTENDED: "Not Extended"
  610. }
  611.  
  612. weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  613. monthname = [None,
  614.              'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  615.              'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  616.